package org.dingyom.org.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dingyom.basics.util.AjaxResult;
import org.dingyom.basics.util.PageList;
import org.dingyom.org.domain.Employee;
import org.dingyom.org.query.EmployeeQuery;
import org.dingyom.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 部门的controller层 返回统一封装成一个AjaxResult
 */
@RestController
@RequestMapping("/employee")
@Api(value = "客户关系管理系统",description = "员工相关的crud")//打在controller类上 对其进行描述
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;
    //增加或修改 Put请求
    @PutMapping
    @ApiOperation(value = "保存和修改")//打在方法上 对方法做描述
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if(employee.getId()!=null){//id存在就是修改
                employeeService.modify(employee);
            }else{//id不存在就是新增
                employeeService.add(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败"+e.getMessage());
        }
    }
    //删除 Delete请求
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")//打在方法上 对方法做描述
    public AjaxResult del(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }
    //查询一个 Get请求
    @GetMapping("/{id}")
    @ApiOperation(value = "查找一个")//打在方法上 对方法做描述
    //@ApiParam(value = "id属性",required = true) 打在属性上 对属性做描述
    public AjaxResult getOne(@ApiParam(value = "查询的ID属性",required = true)@PathVariable Long id){
        try {
            Employee employee = employeeService.getOne(id);
            return AjaxResult.me().setData(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
    //查询所有 Get请求
    @GetMapping
    @ApiOperation(value = "查询所有")//打在方法上 对方法做描述
    public AjaxResult getAll(){
        try {
            List<Employee> list = employeeService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }
    //分页列表 Post请求
    @PostMapping
    @ApiOperation(value = "分页列表")//打在方法上 对方法做描述
    public AjaxResult loadPage(@RequestBody EmployeeQuery query){
        try {
            PageList page = employeeService.getPage(query);
            return AjaxResult.me().setData(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败"+e.getMessage());
        }
    }

    //批量删除 patch请求
    @PatchMapping
    @ApiOperation(value = "批量删除")//打在方法上 对方法做描述
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }
    }
}
